package com.gitee.huxing.algorithmic.daily;

import java.util.HashMap;
import java.util.Map;

/**
 * @Description: 无重复字符的最长子串
 * @ProjectName: algorithmic-pactice
 * @Package: LengthOfLongestSubstring
 * @ClassName: LengthOfLongestSubstring
 * @Author: huxingmale
 * @DateTime: 2022-06-06 8:57
 */
public class LengthOfLongestSubstring {

    public static void main(String[] args) {
        LengthOfLongestSubstring ofLongestSubstring = new LengthOfLongestSubstring();
        String s = "abcabcbb";
        System.out.println(ofLongestSubstring.lengthOfLongestSubstring(s));
    }

    public int lengthOfLongestSubstring(String s) {
        Map<Character, Integer> map = new HashMap<Character, Integer>();
        int left = 0, right = 0, ans = 0;
        for (; right < s.length(); right++){
            if (map.containsKey(s.charAt(right))){
                left = Math.max(left, map.get(s.charAt(right)) + 1);
            }
            map.put(s.charAt(right), right);
            ans = Math.max(ans, right - left + 1);
        }
        return ans;
    }
}
